package ch.usi.inf.pf2.alu.nodes;

import ch.usi.inf.pf2.cell.Data;
import ch.usi.inf.pf2.cell.Value;

/**
 * The Node which represent identifiers
 * 
 * @author Yotam Sharon, Thomas Mantegazzi
 * 
 */

public abstract class IdentifierNode implements Node {

	private final Node[] arguments;
	private boolean isZeroNode;

	public IdentifierNode(final Node[] arguments) {
		this.arguments = arguments;
		isZeroNode = false;
	}

	public final Data evaluate() {
		Data[] data = new Data[arguments.length];
		for (int i = 0; i < data.length; i++) {
			if (arguments[i] instanceof ZeroNode) {
				isZeroNode = true;
			}
			data[i] = arguments[i].evaluate();
		}
		double[] toCompute = new double[data.length];
		for (int i = 0; i < data.length; i++) {
			if (data[i] instanceof Value) {
				toCompute[i] = ((Value) data[i]).getInstanceDouble();
			} else {
				return null;
			}
		}
		return new Value(compute(toCompute), isZeroNode);
	}

	protected abstract double compute(double[] args);
}
